<?php
if (!ob_start("ob_gzhandler")) {
  ob_start();
}

require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';

session_name(SESSION);
session_start();

$__sys = Utils::get_param('__sys', NULL);
$__inst = Utils::get_param('__inst', NULL);
$__file = Utils::get_param('__file', 'index');

if (!isset($_SESSION[SYS . $__sys])) {
  die('Session Not Found');
}

$app_data = $_SESSION[SYS . $__sys];
$inst = $app_data->instances[$__inst];

if (!in_array($inst->__folder, $app_data->plugins)) {
  die('Acceess Denied');
}

if ($app_data->sys->login_required == 1 && (!(isset($app_data->is_authorised)) || $app_data->is_authorised == 0) && !in_array($inst->__folder, $app_data->exceptions)) {
  die('Not Authenticated');
}
?>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php include '../include/common.inc.php'; ?>
    
    <?php 
      if (file_exists(PLGN_DIR . $inst->__folder . '/' . $__file . '.hd.php')) {
        include PLGN_DIR . $inst->__folder . '/' . $__file . '.hd.php';
      }
    ?>
  </head>

  <body class="pure-skin-mine">
    <?php 
      if (file_exists(PLGN_DIR . $inst->__folder . '/' . $__file . '.php')) {
        $orm = new ORM();
        include PLGN_DIR . $inst->__folder . '/' . $__file . '.php';
        $orm->close();
      }
    ?>
  </body>
</html>